package com.animee.week03.slice;

import com.animee.week03.ResourceTable;
import com.animee.week03.bean.User;
import com.animee.week03.db.DBManager;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.rdb.RdbStore;
import ohos.data.resultset.ResultSet;

public class DeleteUserAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    TextField idTf;
    Button delBtn,searchBtn;
    Component itemCt;
    Text tv_id,tv_name,tv_phone,tv_info,tv_email;
    int delId = 0;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_deleteuser);
        initView();
    }

    private void initView() {
         idTf = findComponentById(ResourceTable.Id_del_tf);
         delBtn = findComponentById(ResourceTable.Id_del_btn_del);
         searchBtn = findComponentById(ResourceTable.Id_del_btn);
         itemCt = findComponentById(ResourceTable.Id_del_lyt);
         tv_id = findComponentById(ResourceTable.Id_item_usertv_id);
         tv_name = findComponentById(ResourceTable.Id_item_usertv_name);
         tv_phone = findComponentById(ResourceTable.Id_item_usertv_tel);
         tv_email = findComponentById(ResourceTable.Id_item_usertv_email);
         tv_info = findComponentById(ResourceTable.Id_item_usertv_info);
         delBtn.setClickedListener(this);
         searchBtn.setClickedListener(this);
    }


    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_del_btn:
                String idStr = idTf.getText();
                if (idStr==null||idStr.isEmpty()){
                    return;
                }
                User user = findUserById(idStr);
                showUserInfo(user);

                break;
            case ResourceTable.Id_del_btn_del:

                deleteUserById();
                break;
        }
    }

    private void deleteUserById() {
        RdbStore rdbStore = DBManager.getRdbStore(this);
        String sql = "delete from usertb where id = "+delId;
        rdbStore.executeSql(sql);
        showToast("删除成功");
        itemCt.setVisibility(Component.HIDE);
        delBtn.setVisibility(Component.HIDE);
    }

    private void showUserInfo(User user) {
        if (user == null){
            itemCt.setVisibility(Component.HIDE);
            delBtn.setVisibility(Component.HIDE);
        }else{
            itemCt.setVisibility(Component.VISIBLE);
            delBtn.setVisibility(Component.VISIBLE);
            tv_id.setText(user.getId());
            tv_name.setText(user.getName());
            tv_phone.setText(user.getPhone());
            tv_email.setText(user.getEmail());
            tv_info.setText(user.getInfo());
        }


    }

    private User findUserById(String idStr) {
        RdbStore rdbStore = DBManager.getRdbStore(this);
        String sql = "select * from usertb where id = "+idStr;
        ResultSet rs = rdbStore.querySql(sql, null);
        if (rs.goToNextRow()) {
            int id = rs.getInt(0);
            String name = rs.getString(1);
            String gender = rs.getString(2);
            String phone = rs.getString(3);
            String email = rs.getString(4);
            String info = rs.getString(5);
            User user = new User(id + "", name, phone, email, gender, info);
            delId = id;
            return user;
        }else{
            showToast("没有这个id的客户信息");
            return null;
        }

    }

    private void showToast(String msg) {
        new ToastDialog(this)
                .setText(msg)
                .show();
    }
}
